//
//  STHomeViewController.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STHomeViewController.h"
#import "STRefreshCollectionView.h"

// 模型
#import "STBannerBean.h"
#import "STCommodityBean.h"
#import "STSubjectBean.h"
#import "STBrandBean.h"
#import "STHotGoods.h"
#import "STSellerBean.h"
#import "STHomeBean.h"

#import "STCommodityDetailViewController.h"
#import "STCustomTF.h"
#import "STSearchViewController.h"

#import "STInnerViewController.h"
#import "STSellerDetailViewController.h"


@interface STHomeViewController ()<STBaseViewDelegate>

@property (nonatomic ,strong) STRefreshCollectionView *collectionView;

@end

@implementation STHomeViewController

- (void)initSubviews {
    [super initSubviews];
    
    [self initCollectionView];
    
    [self initNavi];
}
- (void)initCollectionView {
    
    STRefreshCollectionView *collectionView = [STRefreshCollectionView instanceWithFrame:self.view.bounds];
    
    self.collectionView = collectionView;
    
    collectionView.mDelegate = self;
    
    [self.view addSubview:collectionView];
}
- (void)initNavi {
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemLeftOffsetWithTarget:self action:@selector(logo) image:@"小logo" highImage:@"小logo"];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemRightOffsetWithTarget:self action:@selector(messageTriggered) image:@"消息" highImage:@"消息"];
    
    STCustomTF *tf = [STCustomTF instance];
    
    tf.mDelegate = self;
    
    tf.frame = CGRectMake(100 * ST_RATE, 0, ST_SCREEN_WIDTH - 100 * ST_RATE * 2, 40);
    
    self.navigationItem.titleView = tf;
    
    tf.iconImageName = @"搜索图标";
    
    tf.backgroundColor = ST_WHITE_COLOR;
    
    tf.layer.cornerRadius = 5;
    
    tf.layer.borderColor = ST_999999_COLOR.CGColor;
    
    tf.layer.borderWidth = 0.5;
    
    tf.layer.masksToBounds = true;
    
    tf.placeHolder = @"搜索";
    
}

- (void)loadData {
    
    
}
- (void)loadNewData {
    
    //
    @WeakObj(self)
    [[STHttpManager getInstance] requestWithUrlString:ST_HTTP_GETHOME andParams:@{} andStartBlock:^BOOL{
        return true;
    } andSuccessBlock:^(NSString *rescode, NSString *msg, NSString *token, NSString *sysTime, id data, NSString *page) {
        if ([rescode isEqualToString:@"100"]) {
            
            STHomeBean *home = [STHomeBean mj_objectWithKeyValues:data];
            // 轮播图
            NSArray *banner = [STBannerBean mj_objectArrayWithKeyValuesArray:home.tads];
            // 分类
            NSArray *subject = [STSubjectBean mj_objectArrayWithKeyValuesArray:home.cates];
            // 热门商品
            NSArray *hots = [STCommodityBean mj_objectArrayWithKeyValuesArray:home.hots];
            // 热点商家
            NSArray *hotsSeller = [STSellerBean mj_objectArrayWithKeyValuesArray:home.hotsSeller];
            // 品牌推荐
            NSArray *brands = [STBrandBean mj_objectArrayWithKeyValuesArray:home.productors];
            // 底部广告
            NSArray *bads = [STBannerBean mj_objectArrayWithKeyValuesArray:home.bads];
            
            home.tads = banner;
            
            home.cates = subject;
            
            home.hots = hots;
            
            home.hotsSeller = hotsSeller;
            
            home.productors = brands;
            
            home.bads = bads;
            
            [selfWeak.collectionView updateHeader:home];
            
            [selfWeak.collectionView updateData:home];
            
        } else {
            
            [SVProgressHUD showInfoWithStatus:msg];
        }
        [selfWeak.collectionView.mj_header endRefreshing];
        
    } andFailureBlock:^BOOL{
        [selfWeak.collectionView.mj_header endRefreshing];
        return true;
    }];
}
- (void)logo {
    
}
- (void)messageTriggered {
    
    [self onItemClick:STItemClickTypeMessage withData:nil forIndexPath:nil];
    
}
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    switch (itemType) {
        case STItemClickTypeItem:
        {
            
            STHomeBean *home = data;
            
            if (indexPath.section == 0) {
                
                STCommodityBean *commodity = home.hots[indexPath.row];
                
                STCommodityDetailViewController *commodityVC = [STCommodityDetailViewController instance];
                
                commodityVC.identifier = commodity.cid;
                
                [self.navigationController pushViewController:commodityVC animated:true];
                
            } else if (indexPath.section == 1) {
                STBannerBean *banner = home.bads[indexPath.row];
                
                NSLog(@"%@",banner);
            } else {
                
                STSellerBean *seller = home.hotsSeller[indexPath.row];
                
                STSellerDetailViewController *sellerDetail = [STSellerDetailViewController instance];
                
                sellerDetail.identifier = seller.sid;
                
                [self.navigationController pushViewController:sellerDetail  animated:true];
            }
        }
            break;
        case STItemClickTypeBanner:
        {
            
            STBannerBean *banner = data;
            
            switch (banner.type) {
                case STBannerClickTypeWebUrl:
                {
                    STInnerViewController *inner = [STInnerViewController instance];
                    
                    inner.webUrl = banner.webUrl;
                    
                    [self.navigationController pushViewController:inner animated:true];
                    
                }
                    break;
                    
                case STBannerClickTypeSellerId:
                {
                    
                    STSellerDetailViewController *sellerDetail = [STSellerDetailViewController instance];
                    
                    sellerDetail.identifier = banner.aid;
                    
                    [self.navigationController pushViewController:sellerDetail  animated:true];
                    
                }
                    break;
                    
                case STBannerClickTypeCommodityId:
                {
                    STCommodityDetailViewController *commodity = [STCommodityDetailViewController instance];
                    
                    commodity.identifier = banner.aid;
                    
                    [self.navigationController pushViewController:commodity animated:true];
                    
                }
                    break;
                    
                default:
                    break;
            }
            
            
        }
            break;
        case STItemClickTypeBrand:
        {
            STBrandBean *brand = data;
            
            STCommodityDetailViewController *commodity = [STCommodityDetailViewController instance];
            
            [self.navigationController pushViewController:commodity animated:true];
        }
            break;
        case STItemClickTypeSubject:
        {
            STSubjectBean *subject = data;
            
            STCommodityDetailViewController *commodity = [STCommodityDetailViewController instance];
            
            [self.navigationController pushViewController:commodity animated:true];
        }
            break;
        case STItemClickTypeMessage:
        {
            
            
            
        }
            break;
        case STItemClickTypeBeginEditing:
        {
            STSearchViewController *search = [STSearchViewController instance];
            
            [self.navigationController pushViewController:search animated:true];
        }
            break;
            
        default:
            break;
    }
}
@end
